package com.ryder.petmatediarybackend.module.auth.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.ryder.petmatediarybackend.common.response.Result;
import com.ryder.petmatediarybackend.module.auth.task.BlacklistSyncTask;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述: 定时任务管理接口（需要登录 + 平台管理员角色 + 对应权限）
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025/10/28
 */
@Slf4j
@Tag(name = "定时任务管理", description = "手动触发定时任务接口（需要登录 + 平台管理员角色 + 对应权限）")
@RestController
@RequestMapping("/admin/task")
@SaCheckRole("ROLE_PLATFORM_ADMIN")
@SecurityRequirement(name = "Authorization")
@RequiredArgsConstructor
public class AdminTaskController {

    private final BlacklistSyncTask blacklistSyncTask;

    @Operation(summary = "手动触发黑名单同步", description = "立即执行黑名单同步任务，将 Redis 中的黑名单数据同步到 MySQL")
    @PostMapping("/sync-blacklist")
    @SaCheckPermission("task:blacklist:sync")
    public Result<String> syncBlacklist() {
        try {
            log.info("手动触发黑名单同步任务");
            blacklistSyncTask.syncBlacklistToMQ();
            return Result.ok("黑名单同步任务已触发");
        } catch (Exception e) {
            log.error("手动触发黑名单同步任务失败", e);
            return Result.fail("黑名单同步任务触发失败：" + e.getMessage());
        }
    }
}
